import net.lingala.zip4j.core.ZipByte;
import net.lingala.zip4j.core.ZipFile;
import net.lingala.zip4j.exception.ZipException;
import net.lingala.zip4j.io.ZipInputStream;
import net.lingala.zip4j.io.ZipInputStream4Stream;
import net.lingala.zip4j.model.FileHeader;

import java.io.*;
import java.util.List;
import java.util.Map;
import java.util.Random;

/**
 * Created by gelon on 2017/6/30.
 */
public class Entey {

    public static void main(String[] args) throws ZipException, IOException {
        InputStream inputStream = Entey.class.getResourceAsStream("/dtrc.zip");
        ZipByte zipByte = new ZipByte(inputStream);
        if(zipByte.isEncrypted()){
            zipByte.setPassword("qkjr");
        }
        List fileHeaderList = zipByte.getFileHeaders();
        ZipInputStream4Stream is = zipByte.getInputStream((FileHeader) fileHeaderList.get(0));
        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(is,"UTF8"));





//        InputStream inputStream = Entey.class.getResourceAsStream("/dtrc.zip");
//        decrypt(inputStream);
    }


    public static BufferedReader decrypt(InputStream inputStream) throws IOException, ZipException {
        Random random = new Random();
        String tmpPre = "tps".hashCode() + System.currentTimeMillis()+"" + random.nextInt(100000);
        File file = File.createTempFile("/" + tmpPre,"tmp");
        if(!file.exists()){
            file.createNewFile();
        }
        inputStream2File(file,inputStream);
        ZipFile zipFile = new ZipFile(file);
        if (zipFile.isEncrypted()) {
            zipFile.setPassword("qkjr");
        }
        List fileHeaderList = zipFile.getFileHeaders();
        ZipInputStream is = zipFile.getInputStream((FileHeader) fileHeaderList.get(0));
        return new BufferedReader(new InputStreamReader(is,"UTF8"));
    }

    public static void inputStream2File(File file , InputStream inputStream) throws IOException {
        int index = 0;
        byte[] bs = new byte[1024];
        FileOutputStream fos = new FileOutputStream(file);
        while (-1 != (index = inputStream.read(bs))){
            fos.write(bs,0,index);
        }
        inputStream.close();
        fos.close();
    }


}
